package com.example.hissys.controller;

import com.example.hissys.dto.NotificationDetailDTO;
import com.example.hissys.dto.NotificationRequest;
import com.example.hissys.service.NotificationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/api/notifications")
@CrossOrigin(origins = "*")
@Slf4j
public class NotificationController {
    @Autowired
    private NotificationService notificationService;

    /**
     * 获取用户的所有通知
     */
    @GetMapping
    public ResponseEntity<?> getUserNotifications(@RequestParam Integer userId) {
        try {
            List<NotificationDetailDTO> notifications = notificationService.getUserNotifications(userId);
            return ResponseEntity.ok(notifications);
        } catch (Exception e) {
            log.error("Failed to get notifications for user: {}", userId, e);
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 将通知标记为已读
     */
    @PutMapping("/{id}/read")
    public ResponseEntity<?> markAsRead(@PathVariable Integer id, @RequestParam Integer userId) {
        try {
            log.info("Marking notification {} as read for user {}", id, userId);
            boolean success = notificationService.markNotificationAsRead(id, userId);
            if (success) {
                return ResponseEntity.ok().build();
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            log.error("Failed to mark notification as read: {}", id, e);
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }

    /**
     * 创建通知
     */
    @PostMapping
    public ResponseEntity<?> createNotification(@RequestBody NotificationRequest request) {
        try {
            log.info("Received notification request: {}", request);
            notificationService.createNotification(request);
            return ResponseEntity.ok().build();
        } catch (Exception e) {
            log.error("Failed to create notification", e);
            return ResponseEntity.badRequest().body(e.getMessage());
        }
    }
} 